package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.GoodsZoneType;
import com.xinhe.web.entity.QGoods;
import com.xinhe.web.entity.QGoodsPrice;
import com.xinhe.web.reposity.GoodsRepository;
import com.xinhe.web.reposity.GoodsZoneTypeRepository;
import com.xinhe.web.response.GoodsView;
import com.xinhe.web.response.GoodsZoneTypeView;
import com.xinhe.web.util.Snowflake;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import java.util.List;

@Service

public class GoodsZoneTypeService {
    @Autowired
    private GoodsZoneTypeRepository goodsZoneTypeRepository;
    @Autowired
    private GoodsRepository goodsRepository;

    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;

    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    public Result<List<GoodsZoneTypeView>> pageGoodsZoneType() {
        List<GoodsZoneTypeView> views = null;
        views = this.goodsZoneTypeRepository.pageGoodsZoneType();
        if (CollectionUtils.isNotEmpty(views)) {
            for (GoodsZoneTypeView view : views) {
                QGoods qGoods = QGoods.goods;
                QGoodsPrice qGoodsPrice = QGoodsPrice.goodsPrice;
                BooleanBuilder builder = new BooleanBuilder();
                builder.and(qGoods.fGoodsTypeId.eq(view.getId()));
                //builder.and(qGoods.removed.eq(0));
                List<GoodsView> goodsViews = queryFactory.select(
                        Projections.bean(
                                GoodsView.class,
                                qGoods.sellerUserId,
                                qGoods.fGoodsTypeId,
                                qGoods.id,
                                qGoods.goodsZoneType,
                                qGoods.fGoodsZoneTypeId,
                                qGoods.name,
                                qGoods.iconPath,
                                qGoods.dis,
                                qGoods.price,
                                qGoods.sendAddress,
                                qGoods.freightFee,
                                qGoodsPrice.originalPrice,
                                qGoodsPrice.unit
                        )
                ).from(qGoods)
                        .leftJoin(qGoodsPrice).on(qGoods.priceId.eq(qGoodsPrice.id))
                        .where(builder).fetch();
                if (CollectionUtils.isNotEmpty(goodsViews)) {
                    view.setGoodsViews(goodsViews);
                }
            }
        }
        return Result.ok(views);
    }

    public Result delete(Long goodsZoneTypeId) {
        goodsZoneTypeRepository.deleteById(goodsZoneTypeId);
        return Result.ok();
    }

    public Result<GoodsZoneType> findById(Long goodsZoneTypeId) {
        return Result.ok(goodsZoneTypeRepository.findById(goodsZoneTypeId).get());
    }

    public Result saveModel(Long id,String name, String iconPath, Integer seq, String remark) {
        GoodsZoneType goodsZoneType = new GoodsZoneType();
        goodsZoneType.setId(Snowflake.generateId());
        goodsZoneType.setIconPath(iconPath);
        goodsZoneType.setName(name);
        goodsZoneType.setSeq(seq);
        goodsZoneTypeRepository.save(goodsZoneType);
        return Result.ok();
    }
}
